﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Bildverarbeitung;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Drawing.Imaging;

namespace Bildverarbeitung.CGFilter
{
    class FessiAymenFilter : Bildverarbeitung.IFilter
    {
       
        int
           filter = 1;

        Boolean
           trace = false;

        public Bitmap GetImageWithDoor(Bitmap image)
        {

             UnmanagedImage temp_img = new UnmanagedImage(image.LockBits(new Rectangle(0, 0, image.Width, image.Height),ImageLockMode.ReadWrite, image.PixelFormat));
             UnmanagedImage final_img = temp_img.Clone();

            SaturationCorrection f_SaturationCorrection = new SaturationCorrection(-0.8f);
            f_SaturationCorrection.ApplyInPlace(temp_img);

            if(trace) trace_img(temp_img.ToManagedImage(), "Saturation Correction");
            Grayscale f_Grayscale = new Grayscale(0.2125, 0.7154, 0.0721);
            temp_img = f_Grayscale.Apply(temp_img);

            if(trace) trace_img(temp_img.ToManagedImage(), "Grayscale");
            switch (filter)
            {
                case 1:
                    CannyEdgeDetector f_CannyEdgeDetector = new CannyEdgeDetector(20, 21);
                    f_CannyEdgeDetector.ApplyInPlace(temp_img);
                    trace_img(temp_img.ToManagedImage(), "CannyEdgeDetector");
                    break;

                case 2:
                    HomogenityEdgeDetector f_HomogenityEdgeDetector = new HomogenityEdgeDetector();
                    f_HomogenityEdgeDetector.ApplyInPlace(temp_img);
                    trace_img(temp_img.ToManagedImage(), "Homogenity Edge Detector");
                    break;

                default:
                    DifferenceEdgeDetector f_DifferenceEdgeDetector = new DifferenceEdgeDetector();
                    f_DifferenceEdgeDetector.ApplyInPlace(temp_img);
                    trace_img(temp_img.ToManagedImage(), "Difference Edge Detector");
                    break;
            }


            Threshold threshold__ = new Threshold(5);
            threshold__.ApplyInPlace(temp_img);
            if (trace) trace_img(temp_img.ToManagedImage(), "threshold__");

            BlobCounter blob_counter_ = new BlobCounter();
            blob_counter_.MaxHeight = 800;
            blob_counter_.MaxWidth = 400;
            blob_counter_.MinHeight = 90;
            blob_counter_.MinWidth = 90;
            blob_counter_.FilterBlobs = true;
            blob_counter_.ObjectsOrder = ObjectsOrder.Size;
            blob_counter_.ProcessImage(temp_img);

            Blob[] blobCounter = blob_counter_.GetObjectsInformation();

            if (blobCounter.Length > 0)
            {
                Drawing.FillRectangle(final_img, blobCounter[0].Rectangle, Color.Yellow);

                if (blobCounter.Length > 1)
                    Drawing.FillRectangle(final_img,blobCounter[1].Rectangle, Color.Yellow);

                if (blobCounter.Length > 2)
                    Drawing.FillRectangle(final_img, blobCounter[2].Rectangle, Color.Yellow);

                if (blobCounter.Length > 3)
                    Drawing.FillRectangle(final_img, blobCounter[3].Rectangle, Color.Yellow);

                for (int i_ = 0; i_ < blobCounter.Length; i_++)
                {
                    Drawing.FillRectangle(final_img,new Rectangle(blobCounter[i_].Rectangle.X, blobCounter[i_].Rectangle.Y,10, 10), Color.Red);

                    Drawing.FillRectangle(final_img,new Rectangle( blobCounter[i_].Rectangle.X + blobCounter[i_].Rectangle.Width - 10,blobCounter[i_].Rectangle.Y, 10, 10), Color.Red);

                    Drawing.FillRectangle(final_img,new Rectangle(blobCounter[i_].Rectangle.X, blobCounter[i_].Rectangle.Y + blobCounter[i_].Rectangle.Height - 10,10, 10), Color.Red);

                    Drawing.FillRectangle(final_img,new Rectangle( blobCounter[i_].Rectangle.X + blobCounter[i_].Rectangle.Width - 10, blobCounter[i_].Rectangle.Y + blobCounter[i_].Rectangle.Height - 10,10, 10), Color.Red);

                    Drawing.FillRectangle(final_img, new Rectangle( blobCounter[i_].Rectangle.X, blobCounter[i_].Rectangle.Y,2, blobCounter[i_].Rectangle.Height), Color.Green);

                    Drawing.FillRectangle(final_img, new Rectangle( blobCounter[i_].Rectangle.X + blobCounter[i_].Rectangle.Width, blobCounter[i_].Rectangle.Y,2, blobCounter[i_].Rectangle.Height), Color.Green);

                    Drawing.FillRectangle(final_img, new Rectangle(blobCounter[i_].Rectangle.X, blobCounter[i_].Rectangle.Y,blobCounter[i_].Rectangle.Width, 2), Color.Green);

                    Drawing.FillRectangle(final_img, new Rectangle(blobCounter[i_].Rectangle.X,blobCounter[i_].Rectangle.Y + blobCounter[i_].Rectangle.Height,blobCounter[i_].Rectangle.Width, 2), Color.Green);
                }
            }

            if (trace) trace_img(final_img.ToManagedImage(), "res");
            return final_img.ToManagedImage();
        }

        public void trace_img(Bitmap bmp_, string note_)
        {
             String Lin = "./trace_data/";
            bmp_.Save(Lin + note_ + "_" +System.DateTime.Now.Minute + "_" + System.DateTime.Now.Millisecond + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        public String GetName()
        {
            return ("FessiAymen");
        }
    }
}
